package ru.ifmo.ipimenov.ewf_svn.standalone.view.gui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.ifmo.ipimenov.ewf_svn.standalone.controller.SetStateAction;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.StateProvider;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.Document;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.State;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.StateModel;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class StateForm extends JFrame {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(LoginForm.class);

    private Document d;

    private JButton stateButton;

    private JList list;

    private Container contentPane;

    public StateForm(Document d, JList liste) {
        super("Status update");

        this.d = d;

        setPreferredSize(new Dimension(300, 200));
        int yLocation = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2 - getPreferredSize().getHeight() / 2);
        int xLocation = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2 - getPreferredSize().getWidth() / 2);
        setLocation(xLocation, yLocation);

        list = new JList();
        List<State> list2 = StateProvider.getInstance().load(d.getCategory());
        list.setModel(new StateModel(list2));
        list.setSelectedIndex(list2.indexOf(d.getState()));
        log.info(list2.indexOf(d.getState()));

        stateButton = new JButton("Set State");
        stateButton.addActionListener(new SetStateAction(list, d, this, liste));

        contentPane = getContentPane();
        addComponentsToPane();
    }

    private void addComponentsToPane() {
        contentPane.setLayout(new BorderLayout(5, 5));

        if (!(contentPane.getLayout() instanceof BorderLayout)) {
            contentPane.add(new JLabel("Container doesn't use BorderLayout."));
            return;
        }

        contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        contentPane.add(new JScrollPane(list), BorderLayout.CENTER);


        contentPane.add(stateButton, BorderLayout.PAGE_END);
    }
}